/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import java.util.Date;
import java.util.List;
import org.crystalrange.dao.ScriptDao;
import org.crystalrange.model.Script;
import org.crystalrange.model.User;
import org.crystalrange.service.ScriptService;
import org.crystalrange.service.UserService;

public class ScriptServiceImpl implements ScriptService {

    private ScriptDao scriptDao;
    private UserService userService;

    public void setScriptDao(ScriptDao scriptDao) {
        this.scriptDao = scriptDao;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public Script create(String username) {
        
        User currentUser = this.userService.findByUsername(username); 
        
        Script script = new Script();
        script.setCreatedBy(currentUser);
        script.setCreatedDate(new Date());
        
        return script;
    }
    
    public boolean save(Script script, String username) {
        
        User currentUser = this.userService.findByUsername(username); 
        
        script.setUpdatedBy(currentUser);
        script.setUpdatedDate(new Date());
        
        return this.scriptDao.save(script);
    }

    public boolean remove(Script script) {
        return this.scriptDao.remove(script);
    }

    public Script find(Long id) {
        return this.scriptDao.find(id);
    }

    public List<Script> findAll() {
        return this.scriptDao.findAll();
    }

    public SearchResult<Script> searchAndCount(Search search) {
        return this.scriptDao.searchAndCount(search);
    }
    
    public List<Script> search(Search search) {
        return this.scriptDao.search(search);
    }
    
    public Script searchUnique(Search search) {
        return this.scriptDao.searchUnique(search);
    }
}
